<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button
            size="small"
            type="warning"
            @click="$router.push('/import')"
          >导入</el-button>
          <el-button
            size="small"
            type="danger"
            @click="getEmployee"
          >导出</el-button>
          <!-- 如果混入mixin里面的逻辑里对应的值在roles.points里能找到 该功能就显示 否则就不显示 -->
          <el-button
            v-if="checkPermission(userPermission.add)"
            size="small"
            type="primary"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" type="index" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="头像" sortable="" width="100">
            <template v-slot="{ row }">
              <img
                v-imgError="require('@/assets/common/head.jpg')"
                style="width: 100%; border-radius: 50%"
                :src="row.staffPhoto"
                alt=""
                @click="showCode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column
            :formatter="formOfEmployment"
            label="聘用形式"
            prop="formOfEmployment"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="correctionTime" sortable="" />
          <el-table-column label="账户状态" sortable="" />
          <el-table-column label="操作" sortable="" fixed="right" width="240">
            <template v-slot="{ row }">
              <el-button
                type="text"
                size="small"
                @click="$router.push(`/employees/detail/${row.id}`)"
              >查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="showAssignDialog(row.id)">角色</el-button>
              <el-button
                v-if="checkPermission(userPermission.remove)"
                type="text"
                size="small"
                @click="delEmployee(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- :current-page.sync="page.page" .sync组件内置方法直接修改数据  -->
          <el-pagination
            layout=" total,prev, pager, next"
            :page-size="page.size"
            :current-page.sync="page.page"
            :total="page.total"
            @current-change="getEmployeeList"
          />
        </el-row>
      </el-card>
    </div>
    <addEmployee :visible.sync="showDialog" />
    <AssignRole :visible.sync="showAssignRole" :employee-id="employeeId" />
    <el-dialog
      title="二维码"
      :visible.sync="QRCodeVisible"
      width="30%"
    >
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogVisible = false"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import addEmployee from './components/addEmployee.vue'
import AssignRole from './components/AssignRole'
import EmployeeEnum from '@/constant/employees'
import PermissionEnum from '@/constant/permisssion'
import imgError from '@/assets/common/head.jpg'
import QRCode from 'qrcode'
import PermissionMixin from '@/mixin/permission'
export default {
  components: {
    addEmployee,
    AssignRole
  },
  mixins: [PermissionMixin],
  data() {
    return {
      userPermission: PermissionEnum.user,
      imgError,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      list: [],
      showDialog: false,
      showAssignRole: false,
      QRCodeVisible: false,
      employeeId: ''
    }
  },

  created() {
    this.getEmployeeList()
  },

  methods: {
    showCode(url) {
      if (url) {
        this.QRCodeVisible = true
        this.$nextTick(() => {
          QRCode.toCanvas(this.$refs.myCanvas, url)
        })
      }
    },
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
    },
    formOfEmployment(row, column, cellValue, index) {
      // cellValue表示当前这一格的数据
      // if (cellValue === 1) return '正式'
      // if (cellValue === 2) return '非正式'
      // return '未知'
      // 为了后期维护 引入组件使用
      const obj = EmployeeEnum.hireType.find((item) => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    async delEmployee(id) {
      try {
        await this.$confirm('您确定要删除嘛')
        await delEmployee(id)
        this.$message.success('删除成功')
        this.getEmployeeList()
      } catch (e) {
        console.log(e)
      }
    },
    // 导出获得所有员工
    async getEmployee() {
      // 动态引入excel
      const excel = await import('@/vendor/Export2Excel')
      const exportExcelMapPath = EmployeeEnum.exportExcelMapPath
      // 获得所有员工
      const { rows } = await getEmployeeList({
        page: 1,
        size: this.page.total
      })
      // console.log(rows)

      // 获取对象的所有键
      const headers = Object.keys(exportExcelMapPath)
      const data = rows.map((item) => {
        // 外层数组 [[里层数组],[里层数组]...]
        return headers.map((ele) => {
          // 里层数组 [手机号---部门]变成data的第一项
          const val = item[exportExcelMapPath[ele]] // 最终变成二维数组
          if (ele === '聘用形式') {
            return (
              EmployeeEnum.hireType.find((t) => t.id === val)?.value || '未知'
            )
          }
          return val
        })
      })
      // const data = [['13987605566', '狗蛋蛋', '2018/1/1', '正式', '2018/3/1', '1111', '总裁办']]
      excel.export_json_to_excel({
        header: headers,
        data: data,
        filename: '员工信息表'
      })
    },
    showAssignDialog(id) {
      this.employeeId = id
      this.showAssignRole = true
    }
  }
}
</script>

<style scoped lang="less"></style>
